package ysq.xyg.base.extend.ehcache;

import org.apache.commons.lang3.StringUtils;
import ysq.xyg.base.util.SpringApplicationContextUtil;
import ysq.xyg.recovery.model.po.RecoveryLargeCat;
import ysq.xyg.recovery.model.po.RecoveryMiddleCat;
import ysq.xyg.recovery.model.po.RecoverySmallCat;
import ysq.xyg.shop.model.po.ShopBanner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 缓存工具
 *
 * @author taocongcong
 * @create 2019-04-11 18:11
 */
public class EhcacheUtils {

    private static EhCacheService ehCacheService = (EhCacheService) SpringApplicationContextUtil.getBean("ehCacheService");

    public static RecoveryLargeCat selecRecoveryLargeCatById(String id) {
        RecoveryLargeCat recoveryLargeCat = new RecoveryLargeCat();
        List<RecoveryLargeCat> recoveryMiddleCatList = ehCacheService.selectRecoveryLargeCatList();
        if (recoveryMiddleCatList != null && recoveryMiddleCatList.size() > 0) {
            for (RecoveryLargeCat entity : recoveryMiddleCatList) {
                if (StringUtils.equals(entity.getId(), id)) {
                    recoveryLargeCat = entity;
                    break;
                }
            }
        }
        return recoveryLargeCat;
    }

    public static List<RecoveryMiddleCat> selecRecoveryMiddleByLargeIdList(String largeCatId) {
        List<RecoveryMiddleCat> recoveryMiddleCatList = new ArrayList<RecoveryMiddleCat>();
        Map<String, List<RecoveryMiddleCat>> map = ehCacheService.selectRecoveryMiddleCatList();
        if (map != null && map.size() > 0) {
            for (Map.Entry<String, List<RecoveryMiddleCat>> entry : map.entrySet()) {
                if (StringUtils.equals(entry.getKey(), largeCatId)) {
                    recoveryMiddleCatList = entry.getValue();
                    break;
                }
            }
        }
        return recoveryMiddleCatList;
    }

    public static List<RecoveryMiddleCat> selecOnlyRecoveryMiddleByLargeIdList(String largeCatId) {
        List<RecoveryMiddleCat> recoveryMiddleCatList = new ArrayList<RecoveryMiddleCat>();
        Map<String, List<RecoveryMiddleCat>> map = ehCacheService.cacheOnlyRecoveryMiddleCat();
        if (map != null && map.size() > 0) {
            for (Map.Entry<String, List<RecoveryMiddleCat>> entry : map.entrySet()) {
                if (StringUtils.equals(entry.getKey(), largeCatId)) {
                    recoveryMiddleCatList = entry.getValue();
                    break;
                }
            }
        }
        return recoveryMiddleCatList;
    }

    public static List<RecoverySmallCat> selecRecoverySmallCatByMiddleIdList(String middleCatId) {
        List<RecoverySmallCat> recoverySmallCatList = new ArrayList<RecoverySmallCat>();
        Map<String, List<RecoverySmallCat>> map = ehCacheService.selectRecoverySmallCatList();
        if (map != null && map.size() > 0) {
            for (Map.Entry<String, List<RecoverySmallCat>> entry : map.entrySet()) {
                if (StringUtils.equals(entry.getKey(), middleCatId)) {
                    recoverySmallCatList = entry.getValue();
                    break;
                }
            }
        }
        return recoverySmallCatList;
    }

    public static List<ShopBanner> selectShopBannerByTypeList(String type) {
        List<ShopBanner> shopBannerList = new ArrayList<ShopBanner>();
        List<ShopBanner> allShopBannerList = ehCacheService.selectShopBannerList();
        if (allShopBannerList != null && allShopBannerList.size() > 0) {
            for (ShopBanner entity : allShopBannerList) {
                if (StringUtils.equals(type, entity.getType())) {
                    shopBannerList.add(entity);
                }
            }
        }
        return shopBannerList;
    }
}
